import pathToRegexp from 'path-to-regexp/index';
import {getProduction,getProductionList,downloadUrl} from 'services/app';
import {TimeSpan} from '../../utils';
import { routerRedux } from 'dva/router';
export default {
    namespace: 'production',
    state: {
        allobj:[],
        record:{},
        pages:1,
        tabsNum:'000',
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
            dispatch({type:"getMonthTime",payload:{}});
        },
        setup ({ dispatch }) {

        },
    },
    effects: {
        //查看登录状态
        *getProduction ({ payload }, { call, put }){
            const {data,total} = yield call(getProduction, payload);
            yield put({ type: 'save', payload: {getProductionData:data,total:total,pages:payload.pageNumber}});
        },
        //获取时间
        *getMonthTime ({ payload }, { put }){
            let timeStr = [];
            for (let i = 1;i < 13; i++){
                timeStr.push({id:i,name:`${new Date().format('yyyy')}-${i>=10?i:'0'+i}`});
            }
            yield put({type:'save',payload:{allobj:timeStr}});
        },
        //前往详情
        *goList ({ payload }, { call, put }){
            const {data} = yield call(getProductionList, payload);
            localStorage.setItem('id',payload.id);
            if(location.pathname != '/production_list'){
                yield put(routerRedux.push('/production_list'));
            }
            yield put({ type: 'save', payload: {getProductionListData:data}});
        },
        //导出巡检记录
        *download ({ payload }, { call, put }){
            const data = yield call(downloadUrl, payload);
            if(data.code == 0){
                window.open(data.data);
            }
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
